package com.pig4cloud.pig.qc.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.qc.entity.LabelEntity;
import com.pig4cloud.pig.qc.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 标签管理
 * @author QC
 *
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/carLabel" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class LabelController {

	private final  LabelService labelService;

	/**
	 * 分页查询
	 */
	@GetMapping("/page" )
	@PreAuthorize("@pms.hasPermission('qc_carLabel_view')" )
	public R getLabelPage(@ParameterObject Page page, @ParameterObject LabelEntity label) {
		LambdaQueryWrapper<LabelEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(StrUtil.isNotBlank(label.getLabelName()),LabelEntity::getLabelName,label.getLabelName());
		return R.ok(labelService.page(page, wrapper));
	}


	/**
	 * 通过id查询
	 */
	@GetMapping("/{labelId}" )
	@PreAuthorize("@pms.hasPermission('qc_carLabel_view')" )
	public R getById(@PathVariable("labelId" ) Long labelId) {
		return R.ok(labelService.getById(labelId));
	}

	/**
	 * 新增标签管理
	 */
	@PostMapping
	@PreAuthorize("@pms.hasPermission('qc_carLabel_add')" )
	public R save(@RequestBody LabelEntity label) {
		if (!labelService.selectLabelName(label.getLabelName(), label.getLabelId())){
			if (!labelService.selectColor(label.getColor(), label.getLabelId())){
				return R.ok(labelService.save(label));
			}else {
				return R.failed(label,"背景色已存在");
			}
		}else {
			return R.failed(label,"标签名已存在");
		}
	}

	/**
	 * 修改标签管理
	 */
	@PutMapping
	@PreAuthorize("@pms.hasPermission('qc_carLabel_edit')" )
	public R updateById(@RequestBody LabelEntity label) {
		if (!labelService.selectLabelName(label.getLabelName(), label.getLabelId())){
			if (!labelService.selectColor(label.getColor(), label.getLabelId())){
				return R.ok(labelService.updateById(label));
			}else {
				return R.failed(label,"背景色已存在");
			}
		}else {
			return R.failed(label,"标签名已存在");
		}
	}

	/**
	 *删除
	 */
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('qc_carLabel_del')" )
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(labelService.removeBatchByIds(CollUtil.toList(ids)));
//		return R.ok(labelService.removeLabel(CollUtil.toList(ids)));

	}

}
